﻿using System;
using System.Windows.Media;

namespace Gmantis.Controls.Chart
{
    internal class ClrTools
    {
        // Methods
        public static Color ColorFromHSL(double h, double s, double l)
        {
            if (l == 0.0)
            {
                return Colors.Black;
            }
            if (s == 0.0)
            {
                byte r = (byte)(255.0 * l);
                return Color.FromArgb(0xff, r, r, r);
            }
            h /= 360.0;
            double num2 = (l <= 0.5) ? (l * (1.0 + s)) : ((l + s) - (l * s));
            double num3 = (2.0 * l) - num2;
            double[] numArray = new double[] { h + 0.33333333333333331, h, h - 0.33333333333333331 };
            double[] numArray2 = new double[3];
            for (int i = 0; i < 3; i++)
            {
                if (numArray[i] < 0.0)
                {
                    numArray[i]++;
                }
                if (numArray[i] > 1.0)
                {
                    numArray[i]--;
                }
                if ((6.0 * numArray[i]) < 1.0)
                {
                    numArray2[i] = num3 + (((num2 - num3) * numArray[i]) * 6.0);
                }
                else if ((2.0 * numArray[i]) < 1.0)
                {
                    numArray2[i] = num2;
                }
                else if ((3.0 * numArray[i]) < 2.0)
                {
                    numArray2[i] = num3 + (((num2 - num3) * (0.66666666666666663 - numArray[i])) * 6.0);
                }
                else
                {
                    numArray2[i] = num3;
                }
                numArray2[i] = Math.Round((double)(255.0 * numArray2[i]));
            }
            return Color.FromArgb(0xff, (byte)numArray2[0], (byte)numArray2[1], (byte)numArray2[2]);
        }

        public static Color Darken(Color clr)
        {
            float hue = GetHue(clr);
            float saturation = GetSaturation(clr);
            float num3 = GetBrightness(clr) - 0.2f;
            if (num3 < 0f)
            {
                num3 = 0f;
            }
            return ColorFromHSL((double)hue, (double)saturation, (double)num3);
        }

        public static float GetBrightness(Color clr)
        {
            float num = ((float)clr.R) / 255f;
            float num2 = ((float)clr.G) / 255f;
            float num3 = ((float)clr.B) / 255f;
            float num4 = num;
            float num5 = num;
            if (num2 > num4)
            {
                num4 = num2;
            }
            if (num3 > num4)
            {
                num4 = num3;
            }
            if (num2 < num5)
            {
                num5 = num2;
            }
            if (num3 < num5)
            {
                num5 = num3;
            }
            return ((num4 + num5) / 2f);
        }

        public static float GetHue(Color clr)
        {
            if ((clr.R == clr.G) && (clr.G == clr.B))
            {
                return 0f;
            }
            float num = ((float)clr.R) / 255f;
            float num2 = ((float)clr.G) / 255f;
            float num3 = ((float)clr.B) / 255f;
            float num4 = 0f;
            float num5 = num;
            float num6 = num;
            if (num2 > num5)
            {
                num5 = num2;
            }
            if (num3 > num5)
            {
                num5 = num3;
            }
            if (num2 < num6)
            {
                num6 = num2;
            }
            if (num3 < num6)
            {
                num6 = num3;
            }
            float num7 = num5 - num6;
            if (num == num5)
            {
                num4 = (num2 - num3) / num7;
            }
            else if (num2 == num5)
            {
                num4 = 2f + ((num3 - num) / num7);
            }
            else if (num3 == num5)
            {
                num4 = 4f + ((num - num2) / num7);
            }
            num4 *= 60f;
            if (num4 < 0f)
            {
                num4 += 360f;
            }
            return num4;
        }

        public static float GetSaturation(Color clr)
        {
            float num = ((float)clr.R) / 255f;
            float num2 = ((float)clr.G) / 255f;
            float num3 = ((float)clr.B) / 255f;
            float num4 = num;
            float num5 = num;
            if (num2 > num4)
            {
                num4 = num2;
            }
            if (num3 > num4)
            {
                num4 = num3;
            }
            if (num2 < num5)
            {
                num5 = num2;
            }
            if (num3 < num5)
            {
                num5 = num3;
            }
            if (num4 == num5)
            {
                return 0f;
            }
            float num6 = (num4 + num5) / 2f;
            if (num6 <= 0.5)
            {
                return ((num4 - num5) / (num4 + num5));
            }
            return ((num4 - num5) / ((2f - num4) - num5));
        }

        public static Color Lighten(Color clr)
        {
            float hue = GetHue(clr);
            float saturation = GetSaturation(clr);
            float num3 = GetBrightness(clr) + 0.2f;
            if (num3 > 0.95)
            {
                num3 = 0.95f;
            }
            return ColorFromHSL((double)hue, (double)saturation, (double)num3);
        }
    }
}
